package com.crm.customer.controller;


import com.crm.common.pojo.Customer;
import com.crm.customer.service.CustomerService;
import com.crm.global.exception.ExceptionEnum;
import com.crm.global.result.Message;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/customer")
@Api(tags = "客户信息管理")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true,defaultValue = "1",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,defaultValue = "1",dataType = "int",paramType = "query")
    })
    @ApiOperation(value = "查询所有客户信息",notes = "根据条件(名字，等级，地址)查询")
    @PostMapping("/findAll")
    public ResponseEntity<Message<PageInfo<Customer>>> findAll(int page, int size, @RequestBody(required = false)  Customer customer) {
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS,customerService.findAll(page,size,customer)));
    }

    @ApiOperation("添加客户信息")
    @PostMapping("/save")
    public ResponseEntity<Message<String>> save(@RequestBody  Customer customer) {
        customerService.saveOrUpdate(customer);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

    @ApiOperation("修改客户信息")
    @PostMapping("/update")
    public ResponseEntity<Message<String>> update(@RequestBody Customer customer) {
        customerService.saveOrUpdate(customer);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }

    @ApiOperation("删除客户信息")
    @PostMapping("/delete")
    @ApiImplicitParam(value = "客户id",name = "id",dataType = "long",paramType = "query",required = true)
    public ResponseEntity<Message<String>> remove(Long id) {
        customerService.delete(id);
        return ResponseEntity.ok(new Message<>(ExceptionEnum.SUCCESS));
    }
}
